package com.zhanghu.backendZray.controller.alipay;

import com.zhanghu.backendZray.annotation.LoginNoNeed;
import com.zhanghu.backendZray.manager.alipay.AlipayManager;
import com.zhanghu.backendZray.pojo.dto.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 支付宝-当面付
 *
 * @author ZhangDong
 * @date 2024/8/8 15:35
 */
@Controller
@RequestMapping("alipay")
@Slf4j
@RequiredArgsConstructor
public class AlipayController {

    private final AlipayManager alipayManager;

    /**
     * 当面付预下订单
     */
    @RequestMapping(value = "trade-precreate", produces = MediaType.IMAGE_JPEG_VALUE)
    @ResponseBody
    public byte[] tradePrecreate() throws IOException {
        return alipayManager.tradePrecreate();
    }


    /**
     * 支付宝官方支付回调（扫码支付）
     */
    @RequestMapping("trade-notify")
    @ResponseBody
    @LoginNoNeed
    public String tradeNotify(HttpServletRequest request) {
        return alipayManager.tradeNotify(request);
    }

    /**
     * 订单支付状态轮询
     * 当前用户最近的一笔订单是否已支付
     */
    @GetMapping("latest-trade-status-get")
    @ResponseBody
    public ApiResponse<Boolean> latestTradeStatusGet() {
        boolean success = alipayManager.latestTradeStatusGet();
        return ApiResponse.ok(success);
    }

}
